﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Amazon.Services.Properties;

namespace Amazon.Services.S3
{
    public static class Path
    {
        #region Static Variables
        private static readonly Regex RegEx = new Regex(Resources.PathRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

        public static readonly char AltDirectorySeparatorChar = '/';
        public static readonly char DirectorySeparatorChar = '\\';
        public static readonly char VolumeSeparatorChar = ':';
        #endregion

        internal static String NormalizePath(String path)
        {
            return path;
        }

        internal static void CheckInvalidPathChars(String path)
        {
            for (int i = 0; i < path.Length; i++)
            {
                int num2 = path[i];
                if (((num2 == 0x22) || (num2 == 60)) || (((num2 == 0x3e) || (num2 == 0x7c)) || (num2 < 0x20)))
                    throw new ArgumentException("Invalid path characters");
            }
        }

        public static String GetDriveName(String path)
        {
            if (String.IsNullOrEmpty(path))
                return null;

            Path.CheckInvalidPathChars(path);
            Match match = Path.RegEx.Match(path);
            if (match.Success && match.Groups["bucket"].Success)
                return match.Groups["bucket"].Value;
            return null;
        }

        public static Boolean IsPathRooted(String path)
        {
            if (String.IsNullOrEmpty(path))
                return false;

            Path.CheckInvalidPathChars(path);
            Match match = Path.RegEx.Match(path);
            return match.Success && match.Groups["bucket"].Success;
        }

        public static String Combine(String path1, String path2)
        {
            if ((path1 == null) || (path2 == null))
                throw new ArgumentNullException((path1 == null) ? "path1" : "path2");

            // validate that the paths don't have any invalid characters in them
            Path.CheckInvalidPathChars(path1);
            Path.CheckInvalidPathChars(path2);

            if (path2.Length == 0)
                return path1;
            else if (path1.Length == 0)
                return path2;
            if (Path.IsPathRooted(path2))
                return path2;

            path1 = path1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            path2 = path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            Char ch = path1[path1.Length - 1];
            if (((ch != Path.DirectorySeparatorChar) && (ch != Path.AltDirectorySeparatorChar)) && (ch != Path.VolumeSeparatorChar))
                return path1 + Path.DirectorySeparatorChar + path2;
            return path1 + path2;
        }

        public static String GetDirectoryName(String path)
        {
            if (String.IsNullOrEmpty(path))
                return null;

            Path.CheckInvalidPathChars(path);
            path = Path.NormalizePath(path);

            Match match = Path.RegEx.Match(path);
            if (match.Success && match.Groups["path"].Success)
                return System.IO.Path.GetDirectoryName(match.Groups["path"].Value);
            return null;
        }

        public static String GetExtension(String path)
        {
            if (String.IsNullOrEmpty(path))
                return null;

            Path.CheckInvalidPathChars(path);
            path = Path.NormalizePath(path);

            Match match = Path.RegEx.Match(path);
            if (match.Success && match.Groups["name"].Success)
                return System.IO.Path.GetExtension(match.Groups["name"].Value);
            return System.IO.Path.GetExtension(path);
        }

        public static Boolean HasExtension(String path)
        {
            if (String.IsNullOrEmpty(path))
                return false;

            Path.CheckInvalidPathChars(path);
            path = Path.NormalizePath(path);

            Match match = Path.RegEx.Match(path);
            if (match.Success && match.Groups["name"].Success)
                return System.IO.Path.HasExtension(match.Groups["name"].Value);
            return System.IO.Path.HasExtension(path);
        }

        public static String GetFileName(String path)
        {
            if (String.IsNullOrEmpty(path))
                return null;

            Path.CheckInvalidPathChars(path);
            path = Path.NormalizePath(path);

            Match match = Path.RegEx.Match(path);
            if (match.Success && match.Groups["name"].Success)
                return System.IO.Path.GetFileName(match.Groups["name"].Value);
            return System.IO.Path.GetFileName(path);
        }

        public static String GetFileNameWithoutExtension(String path)
        {
            if (String.IsNullOrEmpty(path))
                return null;

            Path.CheckInvalidPathChars(path);
            path = Path.NormalizePath(path);

            Match match = Path.RegEx.Match(path);
            if (match.Success && match.Groups["name"].Success)
                return System.IO.Path.GetFileNameWithoutExtension(match.Groups["name"].Value);
            return System.IO.Path.GetFileNameWithoutExtension(path);
        }

        public static Char[] GetInvalidFileNameChars()
        {
            return System.IO.Path.GetInvalidFileNameChars();
        }

        public static Char[] GetInvalidPathChars()
        {
            return System.IO.Path.GetInvalidPathChars();
        }

        public static String GetTempFileName()
        {
            return System.IO.Path.GetTempFileName();
        }

        public static String GetTempPath()
        {
            return System.IO.Path.GetTempPath();
        }
    }
}
